/*
 * ConfigurationFrame.java
 *
 * Created on December 11, 2003, 10:19 AM
 */

package gov.nist.applet.phone.ua.gui;

import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import gov.nist.applet.phone.ua.*;

/**
 * 
 * @author DERUELLE Jean
 */
public class ConfigurationFrame extends javax.swing.JFrame
{
	MessengerManager sipMeetingManager;
	Configuration configuration;
	ServerInfoXmlManager serverInfoManager;
	String path = "xmlSource/ServerInfo.xml";

	/** Creates new form ConfigurationFrame */
	public ConfigurationFrame(MessengerManager sipMeetingManager)
	{
		try
		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			SwingUtilities.updateComponentTreeUI(this);
		}
		catch (Exception exe)
		{
			exe.printStackTrace();
		}
		serverInfoManager = new ServerInfoXmlManager(path);
		this.sipMeetingManager = sipMeetingManager;
		configuration = sipMeetingManager.getMessageListener()
				.getConfiguration();
		initComponents();
		this.setSize(400, 300);
		this.setResizable(false);
		// buttonGroup1.add(jRadioButton1);
		// buttonGroup1.add(jRadioButton2);
		jTextField1.setText(configuration.name);
		jTextField2.setText(configuration.outboundProxy);
		jTextField3.setText(String.valueOf(configuration.proxyPort));
		passwordField.setText(configuration.password);
		confirmPasswordField.setText(configuration.password);
		// if(configuration.mediaTransport.equals("udp")){
		// jRadioButton1.setSelected(true);
		// jRadioButton2.setSelected(false);
		// }
		// else{
		// jRadioButton1.setSelected(false);
		// jRadioButton2.setSelected(true);
		// }
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	private void initComponents()
	{// GEN-BEGIN:initComponents
		// buttonGroup1 = new javax.swing.ButtonGroup();
		jPanel1 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		passwordLabel = new javax.swing.JLabel();
		confirmPasswordLabel = new javax.swing.JLabel();
		;
		passwordField = new javax.swing.JPasswordField();
		confirmPasswordField = new javax.swing.JPasswordField();
		;
		// jLabel4 = new javax.swing.JLabel();
		jTextField1 = new javax.swing.JTextField();
		jTextField2 = new javax.swing.JTextField();
		jTextField3 = new javax.swing.JTextField();
		// jRadioButton1 = new javax.swing.JRadioButton();
		// jRadioButton2 = new javax.swing.JRadioButton();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();

		getContentPane().setLayout(null);

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Configuration");
		addWindowListener(new java.awt.event.WindowAdapter()
		{
			public void windowClosing(java.awt.event.WindowEvent evt)
			{
				exitForm(evt);
			}
		});

		jPanel1.setLayout(null);

		jLabel1.setText("username :");
		jPanel1.add(jLabel1);
		jLabel1.setBounds(20, 20, 50, 20);

		jLabel2.setText("Proxy Address :");
		jPanel1.add(jLabel2);
		jLabel2.setBounds(20, 60, 110, 20);

		jLabel3.setText("Proxy Port :");
		jPanel1.add(jLabel3);
		jLabel3.setBounds(20, 100, 110, 20);

		passwordLabel.setText("password:");
		jPanel1.add(passwordLabel);
		passwordLabel.setBounds(20, 140, 110, 20);

		confirmPasswordLabel.setText("confirm password:");
		jPanel1.add(confirmPasswordLabel);
		confirmPasswordLabel.setBounds(20, 180, 110, 20);

		// jLabel4.setText("Type of audio conversation :");
		// jPanel1.add(jLabel4);
		// jLabel4.setBounds(20, 140, 300, 20);

		jPanel1.add(jTextField1);
		jTextField1.setBounds(140, 20, 150, 20);

		jPanel1.add(jTextField2);
		jTextField2.setBounds(140, 60, 150, 20);

		jPanel1.add(jTextField3);
		jTextField3.setBounds(140, 100, 150, 20);

		jPanel1.add(passwordField);
		passwordField.setBounds(140, 140, 150, 20);

		jPanel1.add(confirmPasswordField);
		confirmPasswordField.setBounds(140, 180, 150, 20);

		// jRadioButton1.setText("Real Time");
		// jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
		// public void actionPerformed(java.awt.event.ActionEvent evt) {
		// jRadioButton1ActionPerformed(evt);
		// }
		// });

		// jPanel1.add(jRadioButton1);
		// jRadioButton1.setBounds(50, 170, 110, 24);

		// jRadioButton2.setText("Voice Messaging");
		// jPanel1.add(jRadioButton2);
		// jRadioButton2.setBounds(180, 170, 200, 24);

		jButton1.setText("Apply changes");
		jButton1.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent evt)
			{
				jButton1ActionPerformed(evt);
			}
		});

		jPanel1.add(jButton1);
		jButton1.setBounds(50, 220, 140, 26);

		jButton2.setText("Cancel");
		jButton2.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent evt)
			{
				jButton2ActionPerformed(evt);
			}
		});

		jPanel1.add(jButton2);
		jButton2.setBounds(240, 220, 81, 26);

		getContentPane().add(jPanel1);
		jPanel1.setBounds(0, 0, 400, 300);

		pack();
	}// GEN-END:initComponents

	// private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt)
	// {//GEN-FIRST:event_jRadioButton1ActionPerformed
	// // Add your handling code here:
	// }//GEN-LAST:event_jRadioButton1ActionPerformed

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
	{// GEN-FIRST:event_jButton2ActionPerformed
		// Add your handling code here:
		this.dispose();
	}// GEN-LAST:event_jButton2ActionPerformed

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
	{// GEN-FIRST:event_jButton1ActionPerformed
		// Add your handling code here:
		for (int i = 0; i < 5; i++)
			serverInfoManager.delXmlCode();
		serverInfoManager.addXmlCode("name", jTextField1.getText());
		serverInfoManager.addXmlCode("password", passwordField.getText());
		serverInfoManager.addXmlCode("proxyaddress", jTextField2.getText());
		serverInfoManager.addXmlCode("proxyport", jTextField3.getText());
		serverInfoManager.addXmlCode("protocol", "udp");
		configuration.userURI = jTextField1.getText() + "@"
				+ jTextField1.getText();
		sipMeetingManager.setUserURI(configuration.userURI);
		configuration.outboundProxy = jTextField2.getText();
		try
		{
			configuration.proxyPort = Integer.parseInt(jTextField3.getText());
		}
		catch (NumberFormatException nfe)
		{
			nfe.printStackTrace();
		}
		// if(jRadioButton1.isSelected())
		configuration.mediaTransport = "udp";
		// else
		// configuration.mediaTransport="tcp";
		sipMeetingManager.getMessageListener().resetOutBoundProxy();
		this.dispose();
	}// GEN-LAST:event_jButton1ActionPerformed

	/** Exit the Application */
	private void exitForm(java.awt.event.WindowEvent evt)
	{// GEN-FIRST:event_exitForm
		this.dispose();
	}// GEN-LAST:event_exitForm

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[])
	{

	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	// private javax.swing.ButtonGroup buttonGroup1;
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	// private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel passwordLabel;
	private javax.swing.JLabel confirmPasswordLabel;
	private javax.swing.JPasswordField passwordField;
	private javax.swing.JPasswordField confirmPasswordField;
	private javax.swing.JPanel jPanel1;
	// private javax.swing.JRadioButton jRadioButton1;
	// private javax.swing.JRadioButton jRadioButton2;
	private javax.swing.JTextField jTextField1;
	private javax.swing.JTextField jTextField2;
	private javax.swing.JTextField jTextField3;
	// End of variables declaration//GEN-END:variables

}
